package com.devbobcorn.nekoration.client;

import com.devbobcorn.nekoration.client.gui.screen.PaintingScreen;
import com.devbobcorn.nekoration.client.gui.screen.PaintingSizeScreen;
import com.devbobcorn.nekoration.client.gui.screen.PaletteScreen;
import com.devbobcorn.nekoration.client.rendering.MusicRenderer;
import com.devbobcorn.nekoration.sounds.ModSounds;
import com.devbobcorn.nekoration.sounds.music.LyricsHelper;
import com.devbobcorn.nekoration.sounds.music.VisualHelper;

import java.awt.Color;
import java.util.logging.Logger;

import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.MusicTicker;
import net.minecraft.util.Hand;

public class ClientHelper {
    public static String NULL = "<null>";
    private static Logger LOGGER = Logger.getLogger("Client Helper");

    public static void showPaletteScreen(Hand hand, byte active, Color[] colors) {
        Minecraft.getInstance().setScreen(new PaletteScreen(hand, active, colors));
    }

    public static void showPaintingSizeScreen(Hand hand, int count) {
        Minecraft.getInstance().setScreen(new PaintingSizeScreen(hand, count));
    }

    public static void showPaintingScreen(int painting) {
        Minecraft.getInstance().setScreen(new PaintingScreen(painting));
    }

    public static void showPaintingScreen(int painting, byte active, Color[] colors) {
        Minecraft.getInstance().setScreen(new PaintingScreen(painting, active, colors));
    }

    public static String chosenMusic = NULL;

    public static void switchTrack(String target){
        if (target.equals(chosenMusic)) return;
        if (ModSounds.CUSTOM_TRACKS.containsKey(target)){ // Switch music...
            chosenMusic = target;
            MusicTicker mt = Minecraft.getInstance().getMusicManager();
            mt.stopPlaying();
            MusicRenderer.reset();
            VisualHelper.reset();
            LyricsHelper.load(chosenMusic);
            LOGGER.info("Music track switched to " + chosenMusic);
        }
    }

    @SuppressWarnings("resource")
    public static void startMusic(){
        if (chosenMusic.equals(NULL))
			return;
        if (!LyricsHelper.ready)
            LyricsHelper.load(chosenMusic);
        MusicRenderer.reset();
        MusicRenderer.start(Minecraft.getInstance().level.getGameTime());
        if (ModSounds.CUSTOM_TRACKS.containsKey(chosenMusic)){
            MusicTicker mt = Minecraft.getInstance().getMusicManager();
            mt.stopPlaying();
            mt.startPlaying(ModSounds.CUSTOM_TRACKS.get(chosenMusic));
            try {
                VisualHelper.startThread(chosenMusic);
            } catch(Exception e) {
                e.printStackTrace();
            }
            LOGGER.info("Playing music: " + chosenMusic);
        }
    }
}
